/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：MemberDataItemDaoImpl.java
 * History:
 *       May 11, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import net.sf.json.JSONObject;

import com.xteam.ecsystem.dao.IMemberDataItemDao;
import com.xteam.ecsystem.entities.BaseInfoItem;
import com.xteam.ecsystem.entities.BaseMemberInfo;
import com.xteam.ecsystem.utils.PageCenterStation;

/**
 * 会员资料项数据访问实现类
 * 
 * @author liuzhi
 *
 */
@Repository
@SuppressWarnings("unchecked")
public class MemberDataItemDaoImpl extends DDLPrefix implements IMemberDataItemDao {
	
	@Autowired
	private BaseDaoImpl baseDao;

	public void delObject(String ids) {
		Class clazz = null;
		try {
			clazz = Class.forName("com.xteam.ecsystem.entities.BaseInfoItem");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		List list = new ArrayList();
		String oId[] = ids.split(",");
		for (String id : oId) {
			Object obj = this.baseDao.get(clazz, id);
			if (obj != null) {
				list.add(obj);
			}
		}
		this.baseDao.deleteAll(list);
	}

	public String getMdiList(int pageIndex, int pageSize) {
		JSONObject jo = new JSONObject();
		try {
			String hql = SQL_FROM + BaseInfoItem.REF + SQL_WHERE +" 1=1 " + SQL_ORDERBY + "biiOrder";
			String countHQL = SQL_SELECT + " count(*) " + SQL_FROM + BaseInfoItem.REF 
					+ SQL_WHERE +" 1=1 ";
			
			PageCenterStation<?> p = (PageCenterStation<?>) this.baseDao
					.queryList(countHQL, hql, null, pageIndex * pageSize, pageSize);
			
			jo = JSONObject.fromObject(p);

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}

	public void saveOrUpdateObject(Object obj, String id) {
		try {
			BaseInfoItem bii = (BaseInfoItem)obj;
			if (id != null && !id.equals("") && id.length() > 0){
				this.baseDao.saveOrUpdate(bii);
			}else {
				this.baseDao.save(bii);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getMdiById(String id) {
		JSONObject jo = new JSONObject();
		try {
			List list = new ArrayList();
			
			String hql = SQL_FROM + BaseInfoItem.REF + SQL_WHERE +" 1=1 " 
					+ SQL_AND + " biiId=?";
			list = this.baseDao.find(hql, new Object[]{id});
			
			jo = JSONObject.fromObject((BaseInfoItem)list.get(0));

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}

	public void updateMdiStateById(String id) {
		Class clazz = null;
		try {
			clazz = Class.forName("com.xteam.ecsystem.entities.BaseInfoItem");
			
			BaseInfoItem bii = (BaseInfoItem)this.baseDao.get(clazz, id);
			if(bii.getBiiState() == 1){
				bii.setBiiState(0);
			}else{
				bii.setBiiState(1);
			}
			
			this.baseDao.saveOrUpdate(bii);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public void updateRegStateById(String id) {
		Class clazz = null;
		try {
			clazz = Class.forName("com.xteam.ecsystem.entities.BaseInfoItem");
			
			BaseInfoItem bii = (BaseInfoItem)this.baseDao.get(clazz, id);
			if(bii.getBiiIsReg() == 1){
				bii.setBiiIsReg(0);
			}else{
				bii.setBiiIsReg(1);
			}
			
			this.baseDao.saveOrUpdate(bii);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public boolean getBmiBybii(String biiIds) {
		boolean isUsed = false;
		List list = new ArrayList();
		String hql = "";
		
		try {
			String oId[] = biiIds.split(",");
			for (String id : oId) {
				hql = SQL_FROM + BaseMemberInfo.REF + SQL_WHERE + "bmiItem = ?";
				list = this.baseDao.find(hql, new Object[]{id});
				
				if (list != null && !list.isEmpty()) {
					isUsed = true;
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return isUsed;
	}

}
